package com.cloud.qualitycontrol.controller;

import com.cloud.qualitycontrol.model.FileFolder;
import com.cloud.qualitycontrol.service.FolderService;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import static com.cloud.qualitycontrol.util.CommonVar.OPERATE_SUCCESS_STR;

/**
 * 处理与质量管理树节点相关联文件夹操作
 */
@RestController
public class FileFolderController {

    @Autowired
    private FolderService folderService;

    @ApiOperation(value = "增加一个folder")
    @PostMapping("/quality/folder/add")
    public FileFolder add(@RequestBody FileFolder fileFolder) {
        if (StringUtils.isBlank(fileFolder.getName())) {
            throw new IllegalArgumentException("Folder 名不能为空");
        }
        return folderService.addFolder(fileFolder);
    }

    @ApiOperation(value = "删除一个folder")
    @DeleteMapping(value = "/quality/folder/{id}")
    public String deleteFolderById(@PathVariable("id") String id) {
        folderService.deleteById(id);
        return OPERATE_SUCCESS_STR;
    }

    @ApiOperation(value = "根据ID查询Folder")
    @GetMapping("/quality/folder/{id}")
    public FileFolder getFolderById(@PathVariable String id){
        return folderService.selectById(id);
    }


    @ApiOperation(value = "更新Folder")
    @PostMapping("/quality/folder/update")
    public FileFolder updateNode(@RequestBody FileFolder fileFolder){
        if (fileFolder.getId() == null){
            throw new IllegalArgumentException("Folder id is null");
        }
        return folderService.updateFolder(fileFolder);
    }

}
